import Destriable from "./Destriable.js";
import Direction from "./direction.js";
import Event from "./event.js";

export default class MoveableEntity extends Destriable {
  constructor(x,y,width,height,imgSrc,life,direction = Direction.TOP,speed = 1){
    super(x,y,width,height,imgSrc,life);
    this.direction = direction;
    this.speed=speed;
    this.sprites = [];
    this.minX = 0;
    this.minY = 0;
    this.maxX = 750;
    this.maxY = 550;
    // this.event = new Event();
  }

  move(direction,cols){
    var {x,y,width,height} = this;
    var next = {x ,y,width,height };
    this.direction = direction;
    switch(this.direction){
      case Direction.TOP:        
        next.y -= this.speed;
        break;
      case Direction.DOWN:
        next.y += this.speed
        break;
      case Direction.LEFT:
        next.x -= this.speed;
        break;
      case Direction.RIGHT:
        next.x += this.speed;
        break;
    }

    this.image.src = this.sprites[this.direction];
    
    // 计算前方是否通行
    this.passable = cols.find(e=>{
      return e.collision(next)
    })

    next.x = next.x <= this.minX ? this.minX : next.x;
    next.y = next.y <= this.minY ? this.minY : next.y;
    // var maxX = 750,maxY = 550;
    next.x = next.x >= this.maxX ? this.maxX : next.x;
    next.y = next.y >= this.maxY ? this.maxY : next.y;

    if(!this.passable){
      Object.assign(this,next);
    }
  }
}